package cn.echase.ecare.yinfayun.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class JSONNodeUtil {

    public static JSONObject findNodeByName(String name, JSONObject date) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode node = objectMapper.readTree(date.toString());
        JsonNode foundNode = findNodeByName(node, name);
        if (foundNode != null) {
            return objectMapper.readValue(foundNode.toString(), JSONObject.class);
        }
        return null;
    }

    private static JsonNode findNodeByName(JsonNode node, String nameToFind) {
        if (node.isObject()) {
            if (node.has("name") && node.get("name").asText().equals(nameToFind)) {
                return node;
            }
            Iterator<String> stringIterator = node.fieldNames();
            while (stringIterator.hasNext()) {
                String fieldName = stringIterator.next();
                JsonNode childNode = node.get(fieldName);
                JsonNode foundNode = findNodeByName(childNode, nameToFind);
                if (foundNode != null) {
                    return foundNode;
                }
            }
        } else if (node.isArray()) {
            for (JsonNode childNode : node) {
                JsonNode foundNode = findNodeByName(childNode, nameToFind);
                if (foundNode != null) {
                    return foundNode;
                }
            }
        }
        return null;
    }

    public static String getValueFromJson(JSONObject resultJson, String keyName){
        if (ObjUtil.isNull(resultJson)){
            return null;
        }

        if (resultJson.containsKey(keyName)){
            return resultJson.getString(keyName);
        }

        return null;
    }

    public static void setValueToJson(JSONObject resultJson, String keyName, String value){
        if (ObjUtil.isNull(resultJson)){
            return;
        }

        if (resultJson.containsKey(keyName)){
            resultJson.put(keyName, value);
        }
    }

    public static JSONObject dataConvert(JSONObject pageContent, JSONObject assessDate, String key) throws Exception {
        JSONObject jsonObject = new JSONObject();
        JSONObject data = new JSONObject();
        Set<String> keySet = assessDate.keySet();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode assessDateNode = objectMapper.readTree(assessDate.toString());
        for (String keyValue : keySet) {
            data.put(keyValue, assessDate.get(keyValue));
            JSONObject assessDate1 = findNodeByName(keyValue, pageContent);
            if (assessDate1 != null && assessDate1.containsKey("optionItems")) {
                JSONArray optionItems = assessDate1.getJSONArray("optionItems");
                if (optionItems != null && !optionItems.isEmpty()) {
                    List<String> values = new ArrayList<>();
                    if (assessDateNode.get(keyValue) != null) {
                        if (assessDateNode.get(keyValue).isArray()) {
                            JSONArray jsonArray = assessDate.getJSONArray(keyValue);
                            values = jsonArray == null ? null : jsonArray.toJavaList(String.class);
                        } else {
                            values.add(assessDate.getString(keyValue));
                        }
                    }
                    for (int l = 0; l < optionItems.size(); l++) {
                        JSONObject item = optionItems.getJSONObject(l);
                        if (values != null && !values.isEmpty() && values.contains(item.getString("value"))) {
                            data.put(keyValue + "_" + l, "☑");
                        } else {
                            data.put(keyValue + "_" + l, "☐");
                        }
                    }
                }
            }
        }

        //计算年龄
        if (StringUtils.isNotEmpty(assessDate.getString("birthDate"))) {
            int age = DateUtil.ageOfNow(assessDate.getString("birthDate"));
            data.put("age", age);
        }
        List<JSONObject> list = new ArrayList<>();
        list.add(data);
        if (StringUtils.isNotEmpty(key)) {
            JSONArray jsonArray = data.getJSONArray(key);
            jsonObject.put("data", jsonArray);
        } else {
            jsonObject.put("data", list);
        }
        return jsonObject;
    }

}
